
import sys
import redis
import time
from datetime import datetime, date

#ip = '172.16.3.216'
ip = '172.16.1.81'

def show() :
	print "show()"
	server = redis.Redis(host=ip, port=6379)
	range = server.zrevrange('RANK', 0, 200 ,withscores=True)
	#range.reverse()
	for s in range :
        	data = "%s\n" % str(s)
        	print data
	
def showall() :
	print "show()"
	server = redis.Redis(host=ip, port=6379)
	range = server.zrevrange('RANK', 0, 10000000 ,withscores=True)
	#range.reverse()
	for s in range :
        	data = "%s\n" % str(s)
        	print data

def count() :
	print "count()"
	server = redis.Redis(host=ip, port=6379)
	range = server.zcount('RANK', 0, 1000000)
	#range.reverse()
	for s in range :
        	data = "%s\n" % str(s)
        	print data

def flushall() :
	print "flushall()"
	server = redis.Redis(host=ip, port=6379)
        range = server.zrevrange('RANK', 0, 200 ,withscores=True)
	for s in range :
                data = "%s\n" % str(s)
                print data
	server.flushall()

def flushallandwritefile() :
	print "flushallandwritefile()"
	server = redis.Redis(host=ip, port=6379)
        range = server.zrevrange('RANK', 0, 200 ,withscores=True)
	filename = "redis_result_%s.txt" % time.strftime("%Y%m%d%H%M%S", time.localtime())
	result = open(filename, 'w')
	for s in range :
                data = "%s\n" % str(s)
                print data
		result.write(data)
	result.close()
        server.flushall()

def usage() :
	print " 1: show redis rank"
	print " 2: flushall redis rank"
	print " 3: flushall and write file redis rank"
	sys.exit()

if __name__ == "__main__" :
	if (len(sys.argv) < 2): usage()

	command = sys.argv[1]
	
	if ("1" == command) : show()
	if ("2" == command) : flushall()
	if ("3" == command) : flushallandwritefile()
	if ("4" == command) : showall()
	

